package com.mdoya.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.mdoya.domain.User;
import com.mdoya.mapper.UserMapper;
import com.mdoya.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;
import java.util.Random;

/**
 * @author mdoya
 * @version 1.0
 * @description:
 */
@Slf4j
@Scope("singleton")
@Transactional(propagation = Propagation.REQUIRED)
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    /**
     * 用户登录
     *
     * @param map
     * @return
     */
    @Override
    public User login(Map<String, String> map) {
        String phone = map.get("phone");
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getPhone,phone);
        User user = userMapper.selectOne(queryWrapper);
        if (user != null){
            return user;
        }
        user = new User();
        user.setPhone(phone);
        user.setName("麦逗呀" + new Random().nextInt(99));
        userMapper.insert(user);
        return user;
    }

    /**
     * 根据用户id获取对象user对象
     *
     * @param userLongId
     * @return
     */
    @Override
    public User findIdUser(Long userLongId) {
        return userMapper.selectById(userLongId);
    }
}
